package org.xgj.zhangben.service.user;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.xgj.zhangben.BaseService;
import org.xgj.zhangben.action.user.UserVO;
import org.xgj.zhangben.dao.UserDAO;
import org.xgj.zhangben.entity.UserEntity;
import org.xgj.zhangben.util.StringUtils;
import org.xgj.zhangben.util.Validate;


import sun.misc.BASE64Decoder;

@SuppressWarnings( { "unused", "deprecation" })
@Scope("prototype")
@Service("userService")
public class UserService extends BaseService {
	@Resource
	private UserDAO userDAO;

	/**
	 * 注销
	 * 
	 * @param userVO
	 * @return
	 */
	public String logout() throws Exception {
		ServletActionContext.getRequest().getSession().invalidate();
		return "logout";
	}

	/**
	 * 登陆
	 * 
	 * @return
	 * @throws Exception
	 */
	public String login() throws Exception {
		String result = "loginSuccess";
		String username = getParam("username");
		String password = getParam("password");
		String verifyCodeParam = getParam("verifyCode");
		String verifyCode = getVerifyCode();
		if (verifyCode != null && verifyCode.equals(verifyCodeParam)) {
			UserEntity userEntity = userDAO
					.uniqueResult(
							" from UserEntity where username=:username and password=:password",
							new String[] { "username", "password" },
							new Object[] { username,
									StringUtils.processPwd(username + password) });
			if (userEntity != null) {
				userEntity.setLastLoginTime(new Date());
				BeanUtils.copyProperties(getLoginedUser(), userEntity);
			} else {
				addErrorInfo("用户不存在");
				result = "loginFaild";
			}
		} else {
			addErrorInfo("验证码错误");
			result = "loginFaild";
		}

		if (havaError()) {
			errorToMsgInRequest();
		}
		return result;
	}

	/**
	 * 注册
	 * 
	 * @return
	 * @throws Exception
	 */
	public String register() throws Exception {
		String result = "registerSuccess";
		String username = getParam("re_username");
		String password = getParam("re_password");
		String repassword = getParam("re_repassword");
		String verifyCodeParam = getParam("re_verifyCode");

		String verifyCode = getVerifyCode();
		if (!Validate.lengGEAndLE(username, 4, 12)) {
			addErrorInfo("用户名的长度必须大于等于4，小于等于12");
		}
		if (!Validate.lengGEAndLE(password, 4, 12)) {
			addErrorInfo("密码的长度必须大于等于4，小于等于12");
		}
		if (password != null && !password.equals(repassword)) {
			addErrorInfo("两次密码不相等");
		}
		if (verifyCode == null || !verifyCode.equals(verifyCodeParam)) {
			addErrorInfo("验证码错误");
		}
		if (havaError()) {
			result = "registerFaild";
		} else {
			if (userDAO.findUserByName(username) == null) {
				UserEntity userEntity = new UserEntity();
				userEntity.setUsername(username);
				userEntity.setPassword(StringUtils.processPwd(username
						+ password));
				userEntity.setRegisterTime(new Date());
				userEntity.setLastLoginTime(new Date());
				userDAO.save(userEntity);
				BeanUtils.copyProperties(getLoginedUser(), userEntity);
			} else {
				addErrorInfo("用户已经存在");
				result = "registerFaild";
			}

		}
		if (havaError()) {
			errorToMsgInRequest();
		}
		return result;
	}
}
